<?php
require_once('lanceur_spip.php');
include_spip('functions/pdk_common');

require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkTest.php');

class TestClassVatCalculator extends PdkTest {
    
    /**
     * AlohacartOrderProductModel
     */
    private $order_product_mdl;
    
    
    /**
     * AlohacartOrderModel
     */
    private $order_mdl;
    
    /**
     * VatCalculator
     */
    private $vat_calculator;
    
    /**
     * EcataloguE Currency Model
     */
    private $currency_mdl;
        
    function __construct(){
        parent::__construct();
                
        $pdk_models = array();
        
        $this->order_product_mdl = pdk_load_class('AlohacartOrderProductModel','classes');
        $pdk_models['AlohacartOrderProductModel'] = $this->order_product_mdl;
        $this->order_mdl = pdk_load_class('AlohacartOrderModel','classes');
        $pdk_models['AlohacartOrderModel'] = $this->order_mdl;
        $this->currency_mdl = pdk_load_class('CurrencyModel','classes');
        $pdk_models['CurrencyModel'] = $this->currency_mdl;
        
        $this->AddPdkModels($pdk_models);
        
        $this->vat_calculator = pdk_load_class('VatCalculator','classes/payment/invoice/');
                        
        $this->dropTestTables = false;
    }
        
    /**
     *  Run before each test
     */
    function setUp() {
        parent::setUp();
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        parent::tearDown();
    }
    
    function __destruct() {
        parent::__destruct();        
    }
    
    function TestCalculateProductsVAT_Add() {
        // Let's change order product #2 id_order, so order #1 will have 2 products 
        $this->order_product_mdl->update_by_id(2, array('id_order'=>1));
        $this->currency_mdl->price_precision = 2;
        $order = $this->order_mdl->select_by_id(1);
                
        $order_products = $this->order_product_mdl->select('id_order=1');
        $order_products_check = $order_products; 
        $this->vat_calculator->CalculateOrderProductsVAT(1, 'add', 20, $order_products, $totals);
        // Products array will be extended with new prices        
        $order_products_check[0]['price_without_vat'] = round(90*$order['currency_rate'], $this->currency_mdl->price_precision);        
        $order_products_check[0]['total_price_without_vat'] = round(90*$order['currency_rate'], $this->currency_mdl->price_precision);
        $order_products_check[0]['total_price_with_vat'] = round(90*$order['currency_rate']*1.2, $this->currency_mdl->price_precision);
        $order_products_check[1]['price_without_vat'] = round(80*$order['currency_rate'], $this->currency_mdl->price_precision);
        $order_products_check[1]['total_price_without_vat'] = round(160*$order['currency_rate'], $this->currency_mdl->price_precision);
        $order_products_check[1]['total_price_with_vat'] = round(160*$order['currency_rate']*1.2, $this->currency_mdl->price_precision);
        $this->assertEqual($order_products_check, $order_products);
        // Totals array will be filled:
        $this->assertEqual(round(300*$order['currency_rate'], $this->currency_mdl->price_precision), $totals['total_with_vat']);
        $this->assertEqual(round(250*$order['currency_rate'], $this->currency_mdl->price_precision), $totals['total_without_vat']);
        $this->assertEqual(round(50*$order['currency_rate'], $this->currency_mdl->price_precision), $totals['total_vat']);
        
    }
    
    function TestCalculateProductsVAT_Deduct() {
        // Let's change order product #2 id_order, so order #1 will have 2 products 
        $this->order_product_mdl->update_by_id(2, array('id_order'=>1));
        $this->currency_mdl->price_precision = 2;
        $order = $this->order_mdl->select_by_id(1);
        
        $order_products = $this->order_product_mdl->select('id_order=1');
        $order_products_check = $order_products; 
        $this->vat_calculator->CalculateOrderProductsVAT(1, 'deduct', 20, $order_products, $totals);
        // Products array will be extended with new prices        
        $order_products_check[0]['price_without_vat'] = 9.37;//round(9.37/*75*$order['currency_rate']*/, $this->currency_mdl->price_precision);        
        $order_products_check[0]['total_price_without_vat'] = 9.37;//round(75*$order['currency_rate'], $this->currency_mdl->price_precision);
        $order_products_check[0]['total_price_with_vat'] = round(75*$order['currency_rate']*1.2, $this->currency_mdl->price_precision);
        $order_products_check[1]['price_without_vat'] = round(66.67*$order['currency_rate'], $this->currency_mdl->price_precision);
        $order_products_check[1]['total_price_without_vat'] = 16.66;//round(133.34*$order['currency_rate'], $this->currency_mdl->price_precision);
        $order_products_check[1]['total_price_with_vat'] = round(133.34*$order['currency_rate']*1.2, $this->currency_mdl->price_precision);        
        $this->assertEqual($order_products_check, $order_products);
        // Totals array will be filled:
        $this->assertEqual(round(250*$order['currency_rate'], $this->currency_mdl->price_precision), $totals['total_with_vat']);
        $this->assertEqual(round(26.03/*208.34*$order['currency_rate']*/, $this->currency_mdl->price_precision), $totals['total_without_vat']);
        $this->assertEqual(round(5.22/*41.66*$order['currency_rate']*/, $this->currency_mdl->price_precision), $totals['total_vat']);
    }
}